package AdminControl;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.HangSanXuat;
import Model.HeDieuHanh;
import Model.Mobile;
import Model.SanPham;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

/**
 * Servlet implementation class AddProduct
 */
public class AddProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProduct() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
        int width = 60;
        int height = width * originalImage.getHeight() / originalImage.getWidth();

        BufferedImage resizedImage = new BufferedImage(width, height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();

        return resizedImage;
    }

    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){

        int width = 60;
        int height = width * originalImage.getHeight() / originalImage.getWidth();

	BufferedImage resizedImage = new BufferedImage(width, height, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, 0, 0, width, height, null);
	g.dispose();
	g.setComposite(AlphaComposite.Src);
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
	RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	g.setRenderingHint(RenderingHints.KEY_RENDERING,
	RenderingHints.VALUE_RENDER_QUALITY);
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	RenderingHints.VALUE_ANTIALIAS_ON);

	return resizedImage;
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");



        MultipartRequest mt = null;
        String encType = "utf-8";
        String folder = getServletContext().getRealPath("\\mobileimage");
        int maxSize = 5 * 1024 * 1024;

        mt = new MultipartRequest(request, folder, maxSize, encType, new DefaultFileRenamePolicy());

        String manufacturerID = mt.getParameter("manufacturer");
        String name = mt.getParameter("name");
        String price = mt.getParameter("price");
        String quantity = mt.getParameter("quantity");
        String image = "mobileimage/" + mt.getParameter("dispName");
        String thumbnail = "mobilethumbnail/" + mt.getParameter("dispName");
        String date = mt.getParameter("date");
        String discount = mt.getParameter("discount");
        String discountPlace = "Áp dụng tại 21 Chùa Bộc";
        String warranty = mt.getParameter("warranty");
        String appendix = mt.getParameter("appendix");
        String salientFeature = mt.getParameter("salient_feature");
        String waveband = mt.getParameter("waveband");
        String size = mt.getParameter("size");
        String weight = mt.getParameter("weight");
        String color = mt.getParameter("color");
        String resolution = mt.getParameter("resolution");
        String screenInfo = mt.getParameter("screen_info");
        String language = mt.getParameter("language");
        String shake = mt.getParameter("shake");
        String ringTone = mt.getParameter("ring_tone");
        String speaker = mt.getParameter("speaker");
        String internalMemory = mt.getParameter("internal_memory");
        String externalMemory = mt.getParameter("external_memory");
        String gprs = mt.getParameter("gprs");
        String edge = mt.getParameter("edge");
        String _3g = mt.getParameter("3g");
        String wlan = mt.getParameter("wlan");
        String bluetooth = mt.getParameter("bluetooth");
        String infraRed = mt.getParameter("infra_red");
        String usb = mt.getParameter("usb");
        String camera = mt.getParameter("camera");
        String operatingSystemID = mt.getParameter("operating_system");
        String cpu = mt.getParameter("cpu");
        String sms = mt.getParameter("sms");
        String browser = mt.getParameter("browser");
        String java = mt.getParameter("java");
        String radio = mt.getParameter("radio");
        String music = mt.getParameter("music");
        String video = mt.getParameter("video");
        String office = mt.getParameter("office");
        String others = mt.getParameter("others");
        String standByTime = mt.getParameter("stand_by_time");
        String talkTime = mt.getParameter("talk_time");
        String state = "Còn hàng";
        String catId = mt.getParameter("catagory");
        
        SanPham s=new Mobile();
       
        
        Mobile sp=(Mobile)s;
        sp.setHangSanXuat(new HangSanXuat(manufacturerID));
        sp.setTenSP(name);
        sp.setCamera(camera);
        sp.setanhDaiDien(thumbnail);
        sp.setBangTan(waveband);
        sp.setBluetooth(bluetooth);
        sp.setBoNhoTrong(internalMemory);
        sp.setCheDoRung(shake);
        sp.setCpu(cpu);
        sp.setEdge(edge);
        sp.setG3(_3g);
        sp.setGiaBan(Integer.parseInt(price));
        sp.setGiaSauKhiGiam(Integer.parseInt(discount));
        sp.setGprs(gprs);
        sp.setHeDieuHanh(new HeDieuHanh(operatingSystemID));
        sp.setHinhAnh(image);
        sp.setHongNgoai(infraRed);
        sp.setJava(java);
        sp.setKichThuoc(size);
        sp.setKichThuocManHinh(resolution);
        sp.setLoa(speaker);
        sp.setMauSac(color);
        
        sp.setMusic(music);
   //     sp.setNgayThang(ngayThang);
        sp.setNgonNgu(language);
        sp.setNhacChuong(ringTone);
        sp.setOffice(office);
        sp.setPhuKienDiKem(appendix);
        sp.setRadio(radio);
        sp.setSms(sms);
        sp.setSoLuong(Integer.parseInt(quantity));
        sp.setTheNhoNgoai(externalMemory);
        sp.setThoiGianBaoHanh(warranty);
        sp.setThoiGianCho(standByTime);
        sp.setThoiGianThoai(talkTime);
        sp.setThongTinManHinh(screenInfo);
        sp.setThongTinNoiBat(salientFeature);
        sp.setTinhNangKhac(others);
        sp.setTrinhDuyet(browser);
        sp.setTrangThai(state);
        sp.setTrongLuong(weight);
        sp.setUsb(usb);
        sp.setVideo(video);
        sp.setWifi(wlan);
        int cat =Integer.parseInt(catId);
        sp.addMobile11(sp, cat);

//        Mobile.addMobile(manufacturerID, name, price, quantity, image,
//                thumbnail, date, discount, discountPlace, warranty, appendix,
//                salientFeature, waveband, size, weight, color, resolution,
//                screenInfo, language, shake, ringTone, speaker, internalMemory,
//                externalMemory, gprs, edge, _3g, wlan, bluetooth, infraRed, usb,
//                camera, operatingSystemID, cpu, sms, browser, java, radio, music,
//                video, office, others, standByTime, talkTime, state, catId);

        //xu ly anh
        try {
            String sourceImage = getServletContext().getRealPath("\\mobileimage") + "\\" +mt.getParameter("dispName");
            String destImage = getServletContext().getRealPath("\\mobilethumbnail") + "\\" +mt.getParameter("dispName");
            BufferedImage originalImage = ImageIO.read(new File(sourceImage));
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImagePng = resizeImageWithHint(originalImage, type);
            ImageIO.write(resizeImagePng, "png", new File(destImage));

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        response.sendRedirect("ProductManage.jsp");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
